package com.seanliao.nav.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.seanliao.nav.common.domain.dto.QuickJumpDTO;
import com.seanliao.nav.common.domain.dto.valid.Add;
import com.seanliao.nav.common.domain.dto.valid.Edit;
import com.seanliao.nav.common.domain.vo.QuickJumpVO;
import com.seanliao.nav.common.exception.BusinessException;
import com.seanliao.nav.common.response.Result;
import com.seanliao.nav.common.response.ResultBuilder;
import com.seanliao.nav.entity.QuickJump;
import com.seanliao.nav.service.IQuickJumpService;
import com.seanliao.nav.util.CommonUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 快速导航管理
 */
@RestController
@RequestMapping("/quick-jump")
@RequiredArgsConstructor
public class QuickJumpController {

    private final IQuickJumpService quickJumpService;


    /**
     * 获取所有快速跳转
     */
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('quickJump:list')")
    public Result<List<QuickJumpVO>> list(@RequestParam(required = false) List<String> searchTags) {
        // 查询体
        LambdaQueryWrapper<QuickJump> lqw = new LambdaQueryWrapper<>();
        // 搜索标签
        if (searchTags != null) {
            List<String> sortedSearchTags = searchTags.stream().sorted().map(o -> "&" + o + "&").toList();
            String searchTagStr = StrUtil.join("%", sortedSearchTags);
            lqw.like(StrUtil.isNotBlank(searchTagStr), QuickJump::getTags, searchTagStr);
        }
        // 查询
        List<QuickJump> list = quickJumpService.list(lqw);
        // 整理数据
        List<QuickJumpVO> vos =
                list.stream().map(quickJump -> {
                    QuickJumpVO vo = BeanUtil.copyProperties(quickJump, QuickJumpVO.class);
                    // 标签
                    vo.setTagList(CommonUtil.parseTags(quickJump.getTags()));
                    return vo;
                }).toList();
        return ResultBuilder.success(vos);
    }

    /**
     * 添加快速跳转
     */
    @PostMapping("/add")
    @PreAuthorize("@pms.hasPermission('quickJump:add')")
    public Result<?> add(@Validated(Add.class) @RequestBody QuickJumpDTO quickJumpDTO) {
        // 检查名称重复
        Assert.isFalse(quickJumpService.count(new LambdaQueryWrapper<QuickJump>()
                .eq(QuickJump::getName, quickJumpDTO.getName())
                .eq(QuickJump::getIsDeleted, false)
        ) > 0, () -> new BusinessException("名称已存在"));
        // 实体
        QuickJump quickJump = BeanUtil.copyProperties(quickJumpDTO, QuickJump.class);
        if (quickJump.getId() == 0L) {
            quickJump.setId(null);
        }
        // 标签整合
        quickJump.setTags(CommonUtil.genTags(quickJumpDTO.getTagList()));
        // 入库
        if (quickJumpService.save(quickJump)) {
            return ResultBuilder.success();
        } else {
            return ResultBuilder.fail("数据写库异常");
        }
    }

    /**
     * 添加快速跳转
     */
    @PostMapping("/edit")
    @PreAuthorize("@pms.hasPermission('quickJump:edit')")
    public Result<?> edit(@Validated(Edit.class) @RequestBody QuickJumpDTO quickJumpDTO) {
        // 检查名称重复
        Assert.isFalse(quickJumpService.count(new LambdaQueryWrapper<QuickJump>()
                .eq(QuickJump::getName, quickJumpDTO.getName())
                .ne(QuickJump::getId, quickJumpDTO.getId())
                .eq(QuickJump::getIsDeleted, false)
        ) > 0, () -> new BusinessException("名称已存在"));
        // 实体
        QuickJump quickJump = BeanUtil.copyProperties(quickJumpDTO, QuickJump.class);
        // 标签整合
        quickJump.setTags(CommonUtil.genTags(quickJumpDTO.getTagList()));
        // 入库
        if (quickJumpService.updateById(quickJump)) {
            return ResultBuilder.success();
        } else {
            return ResultBuilder.fail("数据写库异常");
        }
    }

    /**
     * 删除快速跳转
     */
    @PostMapping("/remove")
    @PreAuthorize("@pms.hasPermission('quickJump:remove')")
    public Result<?> remove(@RequestBody QuickJumpDTO quickJumpDTO) {
        if (quickJumpService.removeById(quickJumpDTO.getId())) {
            return ResultBuilder.success();
        } else {
            return ResultBuilder.fail("数据写库异常");
        }
    }


}
